<?php
// +----------------------------------------------------------------------
// | Yibu Cabinet [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.yibuxinxi.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LGW < 1913630280@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\WechatMenuModel;
use dc\controller\AdminBaseController;
use dragon\Support\Wechat;


class WechatController extends AdminBaseController
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 微信菜单设置列表
     *
     */
    public function index()
    {
        $account = $this->request->param('account');
        $map = [];
        $accounts = Wechat::getWechetAccount();
        $config = config('wechat_account');
        $account ? $map['m1.account'] = $account : $map['m1.account'] = $config;
        $WechatMenuModel = new WechatMenuModel();
        $data = $WechatMenuModel->name('wechat_menu')
            ->alias('m1')
            ->join('wechat_menu m2', 'm1.parent_id=m2.id', 'LEFT')
            ->field('m1.*,m2.name as p_name')
            ->where($map)
            ->select();

        $account ? $this->assign('defaultAccount', $account) : $this->assign('defaultAccount', $config);
        $this->assign('accounts', $accounts);
        $this->assign("wechats", $data);
        return $this->fetch();
    }


    //  搜索出全部上级菜单
    public function parents()
    {
        $WechatMenuModel = new WechatMenuModel();
        //  搜索出全部上级菜单
        $parents = $WechatMenuModel->name('wechat_menu')
            ->alias('m')
            ->field('m.id', 'm.name')
            ->where('parent_id', 'eq', '0')
            ->order('id asc')
            ->select();
        return $parents;
    }

    /**
     * 添加
     *
     */
    public function add()
    {
        $accounts = Wechat::getWechetAccount();
        $config = config('wechat_account');
        $parents = $this->parents();
        $this->assign('defaultAccount', $config);
        $this->assign('accounts', $accounts);
        $this->assign('parents', $parents);
        return $this->fetch();
    }


    /**
     * 添加提交
     *
     */
    public function addPost()
    {
        $WechatMenuModel = new WechatMenuModel();
        $data = $this->request->param();
        $result = $WechatMenuModel->insert($data);
        if ($result) {
            $this->success("添加成功！");
        } else {
            $this->error("添加失败！");
        }
    }

    /**
     * 查看一条
     *
     */
    public function view()
    {
        $WechatMenuModel = new WechatMenuModel();
        $id = $this->request->param('id', '0', 'intval');
        $data = WechatMenuModel::get($id);
        $parents = $this->parents();
        $accounts = Wechat::getWechetAccount();
        $this->assign('accounts', $accounts);
        $this->assign('parents', $parents);
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 编辑提交
     *
     */
    public function editPost()
    {
        $WechatMenuModel = new WechatMenuModel();
        $data = $this->request->param();
        if ($data['type'] == "点击事件") {
            $data['url'] = '';
        }
        if ($data['type'] == "跳转链接") {
        }
        $result = $WechatMenuModel->allowField(true)->isUpdate(true)->save($data);
        if ($result === false) {
            $this->error($WechatMenuModel->getError());
        }

        $this->success("保存成功！", url('Wechat/index'));
    }

    /**
     * 删除
     *
     */
    public function delete()
    {
        $id = $this->request->param("id", 0, 'intval');
        $result = WechatMenuModel::destroy($id);
        if (!empty($result)) {
            $this->success("删除成功！", url('Wechat/index'));
        } else {
            $this->error("删除失败！");
        }
    }

    // 微信菜单提交
    public function push()
    {
        $wechatAccount = $this->request->post('account');
        config('wechat_account', $wechatAccount);
        $result = Wechat::menuPush();
        $this->success($result);
    }

    // 七夕活动
    public function qixi() {
        return $this->fetch();
    }
}
